require('../css/mine.less')
require("../fonts/iconfont.css")
window.$utils.checkLogin();

//调用公共的foot组键
document.ready(function () {
    window.$utils.tabs(2);
    const userId = JSON.parse(window.localStorage.getItem('user')).userId;
    let avatar = ''
    //点击头像图片，触发文件上传的事件
    const userImgBox = document.querySelector("#userImg");
    userImgBox.addEventListener("click", function (e) {
        e.preventDefault();
        upLoadBox.click();
    })

    //监听文件上传事件
    const upLoadBox = document.querySelector("#imgUploadIpt")
    upLoadBox.addEventListener("change", function () {
        // console.log(this.files[0]);
        window.$updateFile('/users/upload', 'imgurl', this.files[0], function (res) {
            console.log(res);
            if (res.status === 0) {
                avatar = "http://139.9.177.51:8099" + res.data;
                //替换头像标签的图片
                userImgBox.src = avatar;

                // 当成功上传之后，立即调用请求修改个人数据
                window.$http.post("/users/userEdit",{
                    userId:userId,
                    imgurl:avatar
                },function(res){
                    if(res.status===0){
                        window.$utils.toast(0,'上传成功')
                    }
                })
            }
        })
    })

    //获取个人数据，动态展示页面
    const nickName = document.querySelector("#nickName");
    const userSign=document.querySelector("#userSign")
    window.$http.get("/users/accountinfo?userId=" + userId, function (res) {
        console.log(res);
        nickName.textContent=res.data.nickname
        userImgBox.src=res.data.imgurl
        userSign.textContent=res.data.sign
    })

    //退出登录
    const logoutBox=document.querySelector("#logout");
    logoutBox.addEventListener("click",function(){
        window.localStorage.clear();
        location.reload()
    })

    let sportsData=document.querySelector("#sportsData")
    sportsData.addEventListener("click",function(){
        location.href="./database.html"
    })
   
  
})
